package com.yookey.common.serializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.yookey.common.bigdecimal.BigDecimalUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.math.BigDecimal;

/**
 * className MoneyDeserializer
 *
 * @Author: Zhang Shao Hui
 * @Description:
 * @Date:Create：in 2019/12/25 16:56
 * @Modified By：
 */
@Slf4j
public class TwoDecimalDeserializer extends JsonDeserializer<Number> {

    private static final long ONE_HUNDRED = 100;

    @Override
    public Number deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        try {
            if(jsonParser!=null){
                return BigDecimalUtil.getBigDecimal(jsonParser.getValueAsString()).multiply(new BigDecimal(ONE_HUNDRED)).longValue();
            } else {
                return null;
            }
        } catch(Exception e) {
            log.error(e.getMessage());
            throw new RuntimeException(e);
        }
    }
}
